package com.itheima.test4.controller;


import com.itheima.bean.Result;
import com.itheima.test4.entity.Answer;
import com.itheima.test4.mapper.AnswerMapper;
import com.itheima.test4.service.AnswerService;
import com.itheima.test4.service.impl.AnswerServiceImpl;
import com.itheima.vo.answerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * <p>
 * 用户角色关系表 前端控制器
 * </p>
 *
 * @author itheima
 * @since 2021-04-04
 */
@RestController
@RequestMapping("/answer")
public class AnswerController {
    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private AnswerServiceImpl answerService;

    /**
     * @description:交卷
     * @author: Java
     * @date: 2021/4/5 15:12
     * @param: id
     * @param: answerList
     * @return: com.itheima.bean.Result
     */
    @RequestMapping("submit")
    public Result submit(int id, @RequestBody ArrayList<Integer> answerList) {
        try {
            Answer answer = new Answer();
            answer.setUserId(id);
//            answerVo.setSubjectId();
            for (Integer answer1 : answerList) {
                System.out.println(answer1);
            }
//            answerMapper.insert()
            return new Result(true, "提交成功", answer);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "提交失败");
        }

    }

    @RequestMapping("tempSubmit")
    public Result tempSubmit(@RequestBody answerVo answervo) {
        try {
            // new一个answer
            Answer answer = new Answer();
            // 从参数获取 用户id
            Integer userId = answervo.getUserId();
            // 从参数获取 数组
//            List<Integer> answerList = answervo.getAnswer();
//            String answerListtoString = StringUtils.join(Arrays.toString(answerList), ",");
            String answerString = answervo.getAnswerString();
            // 从参数获取 题库id
            Integer courseId = answervo.getCourseId();
            // 导入数据
            answer.setUserId(userId);
            answer.setAnswer(answerString);
            answer.setCourseId(courseId);
            // 插入数据
            answerService.insertOrUpdate(answer);
            return new Result(true, "暂时提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "暂时提交失败");
        }

    }


}
